import React from "react";
const verCode = (count = 60) => WrapComponent =>
	class Index extends React.Component {
		constructor(props) {
			super(props);
			this.timer = null;
			this.state = {
				countDown: count, //倒计时默认60
				isClick: true //是否可以点击
			};
		}
		//发送验证码的方法
		sendCode = () => {
			this.start();
		};
		setTimeEndFun = timeEndFun => {
			this.timeEndFun = timeEndFun;
		};
		//开始倒计时
		start = () => {
			this.setState({
				isClick: false
			});
			this.timer = setInterval(() => {
				this.setState(
					{
						countDown: this.state.countDown - 1
					},
					() => {
						this.judgeEnd(this.state.countDown);
					}
				);
			}, 1000);
		};
		//判断是否停止
		judgeEnd = countDown => {
			if (countDown <= 0) {
				clearInterval(this.timer);
				this.timer = null;
				this.setState({
					isClick: true,
					countDown: count
				});

				return;
			}
		};
		componentWillUnmount() {
			clearInterval(this.timer);
			this.timer = null;
		}
		setCountDown = countDown => {
			this.setState({
				countDown
			});
		};
		render() {
			let { countDown, isClick } = this.state;
			return (
				<WrapComponent
					{...this.props}
					verCode={{
						countDown: countDown + "秒后重发",
						isClick,
						sendCode: this.sendCode,
						setTimeEndFun: this.setTimeEndFun,
						setCountDown: this.setCountDown
					}}
				/>
			);
		}
	};
export default verCode;
